package cn.com.b2office.struts2.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.County;
import cn.com.b2office.domain.District;
import cn.com.b2office.domain.Member;
import cn.com.b2office.domain.MemberAddress;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.AddressService;
import cn.com.b2office.service.MemberService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author zhenggang
 * @version Jul 17, 2008 4:42:48 PM comment
 */
public class ModifyBaseInfoAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -6318575584620839500L;

	private Map session = null;

	private String realname = null;

	private String birthday = null;

	private int certfkind = 0;

	private String cardId = null;

	private int quest = 0;

	private String answer = null;

	private int countyid = 0;

	private String addr = null;

	private String mobile = null;

	private String tel = null;

	private String officetel = null;

	private MemberService memberService = null;

	private AddressService addressService = null;

	//

	private Member member = null;

	private MemberAddress address = null;

	private List<District> dists = null;

	private List<County> countys = null;

	public String execute() throws Exception {
		User user = (User) session.get("user");
		if (user == null) {
			return "login";
		}

		Member member = memberService.getMember(user.getMemberId());

		if (member == null)
			return "login";

		// 修改会员信息
		member.setRealname(realname);
		member.setBirthday(birthday);
		member.setCertfkind(certfkind);
		member.setCardid(cardId);
		member.setQuest(quest);
		member.setAnswer(answer);
		memberService.updateMember(member);

		// 地址信息修改
		MemberAddress address = memberService.getRegAddress(user.getMemberId()); // /注册地址是否存在

		if (address == null) {// 如果不存在则创建
			address = new MemberAddress();
			address.setMemberid(member.getMemberid());
			address.setAddrflag(0);
			address.setFlag(1);
		}

		address.setCname(realname);
		County county = addressService.getCounty(countyid);
		if (county != null) {
			address.setProvince(county.getProvince());
			address.setCity(county.getCity());
			address.setDistrict(county.getDistrict());
			address.setCountyid(countyid);
		}
		address.setMobil(mobile);
		address.setTel(tel);
		address.setOfficetel(officetel);
		address.setAddr(addr);

		address.setModifytime(new Date());
		if (address.getAddidno() != null)
			memberService.updateMemberAddress(address);
		else
			memberService.createAddress(address);

		return SUCCESS;

	}

	public void validate() {

		// if (realname == null || realname.replace(" ", "").equals("")) {
		// addFieldError("realName", "真实姓名不能为空");
		// } else if (realname.length() > 50) {
		// addFieldError("realName", "输入过长");
		// }
		//	
		// if (countyid == 0) {
		// addActionError("必须选择所在位置");
		// }
		// if (addr == null || addr.replace(" ", "").equals("")) {
		// addActionError("必须填写地址");
		// }
	}

	public String input() throws Exception {
		User user = (User) session.get("user");
		if (user == null) {
			return "login";
		}

		member = memberService.getMember(user.getMemberId());
		if (member == null)
			return "login";

		address = memberService.getRegAddress(user.getMemberId());

		dists = addressService.listDistricts();
		if (address != null) {
			County county = addressService.getCounty(address.getCountyid());
			countys = addressService.listCountiesByDistrictId(county
					.getDistrict());
		}
		return SUCCESS;

	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setSession(Map session) {
		this.session = session;

	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public int getCertfkind() {
		return certfkind;
	}

	public void setCertfkind(int certfkind) {
		this.certfkind = certfkind;
	}

	public int getCountyid() {
		return countyid;
	}

	public void setCountyid(int countyid) {
		this.countyid = countyid;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getOfficetel() {
		return officetel;
	}

	public void setOfficetel(String officetel) {
		this.officetel = officetel;
	}

	public int getQuest() {
		return quest;
	}

	public void setQuest(int quest) {
		this.quest = quest;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public List<County> getCountys() {
		return countys;
	}

	public void setCountys(List<County> countys) {
		this.countys = countys;
	}

	public List<District> getDists() {
		return dists;
	}

	public void setDists(List<District> dists) {
		this.dists = dists;
	}

	public MemberAddress getAddress() {
		return address;
	}

	public void setAddress(MemberAddress address) {
		this.address = address;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

}
